package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Override
    public void subStock(Long id, Integer num) {
        try {
            Item item = getById(id);
            Integer stock = item.getStock();
            if (stock>=num){
                item.setStock(stock-num);
                if (item.getStock() == 0)item.setStatus(2);
            }
            updateById(item);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}
